package com.cskaoyan.order.controller;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.result.UserLoginMsg;
import com.cskaoyan.mall.commons.util.GetUserInfoUtils;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CancelOrderRequest;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import com.cskaoyan.order.utils.GlobalIdGeneratorUtil;
import com.cskaoyan.order.utils.UserNickNameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author:ZhangShuPeng
 * @Date:2023/01/10/16:14
 * @Description:
 */
@RestController
@RequestMapping("/shopping")
public class OrderController {

    static final String UNIQUEKEY = "uniqueKey";

    @Autowired
    OrderCoreService orderCoreService;

    @Autowired
    OrderQueryService orderQueryService;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    GlobalIdGeneratorUtil globalIdGeneratorUtil;


    @PostMapping("/order")
    public ResponseData order(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {

        UserLoginMsg userInfo = GetUserInfoUtils.getUserInfo(servletRequest);



        //杨宇健 userinfo存入工具类
        UserNickNameUtils.add(userInfo.getUid(), userInfo.getUsername());

        Long uid = userInfo.getUid();
        request.setUserId(uid);

        //设置uniqueKey
        String uniquekey = globalIdGeneratorUtil.getNextSeq(UNIQUEKEY, 3);

        request.setUniqueKey(uniquekey);
        String orderId = orderCoreService.createOrder(request);

        return new ResponseUtil<>().setData(orderId);
    }

    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest, HttpServletRequest httpServletRequest) {

        //校验用户id
        UserLoginMsg userInfo = GetUserInfoUtils.getUserInfo(httpServletRequest);
        Long uid = userInfo.getUid();
        String orderId = cancelOrderRequest.getOrderId();

        orderCoreService.cancelOrder(orderId, uid);

        return new ResponseUtil<>().setData(null);


    }

    /**
     * 查询当前用户的订单列表
     * @param orderListRequest
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/order")
    public ResponseData getOrderList(OrderListRequest orderListRequest, HttpServletRequest httpServletRequest){
        UserLoginMsg userLoginMsg = GetUserInfoUtils.getUserInfo(httpServletRequest);
        Long userId = userLoginMsg.getUid();
        OrderListResponse orderListResponse = orderQueryService.orderList(orderListRequest, userId);
        return new ResponseUtil<>().setData(orderListResponse,"666");
    }

    @DeleteMapping("/order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String id) {

        orderCoreService.deleteOrder(id);

        return new ResponseUtil<>().setData("成功");
    }

    @GetMapping("order/{id}")
    public ResponseData getOrderDetail(@PathVariable("id") String id, HttpServletRequest request){
        UserLoginMsg user = GetUserInfoUtils.getUserInfo(request);
        OrderDetailResponse orderDetailResponse = orderQueryService.orderDetail(id, user);
        return new ResponseUtil<>().setData(orderDetailResponse);
    }

}
